package com.jojo.android.zxlib.base.mvp;

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.GsonBuilder;
import com.jojo.android.zxlib.base.BaseApplication;

import java.net.Proxy;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Converter;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * presenter基类
 *
 * @param <V> 视图
 * @param <M> api模型
 */
public abstract class BasePresenter<V extends BaseView, M> {
    protected M model;
    protected V view;
    private Retrofit retrofit;

    public BasePresenter(V v) {
        this.view = v;
        retrofit = initRetrofit();
        Class<M> mClass = preModel();
        model = retrofit.create(mClass);
    }

    /**
     * 准备网络请求模型
     * @return
     */
    protected abstract Class<M> preModel();

    /**
     * 初始化构建Retrofit
     * @return
     */
    protected  Retrofit initRetrofit(){
        return new Retrofit.Builder()
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(converterFactory())
                .client(buildOkHttpClient())
                .baseUrl(baseUrl())
                .build();
    }

    /**
     * 转化工厂  默认使用谷歌的Gson
     * @return
     */
    protected Converter.Factory converterFactory(){
        return GsonConverterFactory.create(
               new GsonBuilder()
                        //设置时间格式
                        .setDateFormat("yyyy-MM-dd HH:mm:ss")
                        //添加序列化排除规则
                        .addSerializationExclusionStrategy(new ExclusionStrategy() {
                            @Override
                            public boolean shouldSkipField(FieldAttributes f) {
                                //false不排除
                                return false;
                            }

                            @Override
                            public boolean shouldSkipClass(Class<?> clazz) {
                                //false不排除
                                return false;
                            }
                        })
                .create()
        );
    }

    /**
     * 构建OkHttpClient
     */
    protected  OkHttpClient buildOkHttpClient(){
        return new OkHttpClient.Builder()
                //添加日志拦截器
                .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                //不允许代理访问
                .proxy(Proxy.NO_PROXY)
                //超时60秒
                .callTimeout(60, TimeUnit.SECONDS)
                .connectTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                //缓存空间50M
                .cache(new Cache(BaseApplication.getInstance().getCacheDir(), 50 * 1024 * 1024))
                .build();
    }

    protected abstract String baseUrl();

    protected V getView() {
        return view;
    }

    public Retrofit getRetrofit() {
        return retrofit;
    }

    public void destroy() {
        onDestroy();
        view = null;
        model = null;
        retrofit = null;
    }

    protected void onDestroy(){}

}
